package com.honghu.villagepost.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.honghu.villagepost.R;
import com.honghu.villagepost.activity.GoodsDetailsActivity;
import com.honghu.villagepost.entity.dao.GoodsEntity;
import com.honghu.villagepost.util.DensityUtil;
import com.honghu.villagepost.util.GlideUtil;

import java.util.List;

/**
 * Desc:购物车商品列表适配器
 * <p>
 * Author: [李豫]
 * Date: 2021-07-14
 * Copyright: Copyright (c) 2010-2021
 * Updater:
 * Update Time:
 * Update Comments:
 */
public class CatListGoodsAdapter extends BaseQuickAdapter<GoodsEntity, BaseViewHolder> {

    private OnGoodsListAdapterListener mListener;

    public void setOnGoodsListAdapterListener(OnGoodsListAdapterListener listener) {
        this.mListener = listener;
    }

    public CatListGoodsAdapter() {
        super(R.layout.adapter_cat_goods);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position, @NonNull List<Object> payloads) {
        if (payloads != null && payloads.size()>0){
            GoodsEntity item = getItem(position);
            FrameLayout goodsCb = holder.getView(R.id.shop_cb_fl);
            goodsCb.setSelected(item.isSelect());
        }else {
            super.onBindViewHolder(holder, position, payloads);
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, GoodsEntity goods) {
        helper.setText(R.id.tv_name, goods.getName());
        GlideUtil.loadImg(helper.getView(R.id.iv_cover), goods.getMajorPic(), DensityUtil.dp2px(8));
        helper.setText(R.id.tv_price, String.valueOf(goods.getPrice()));
        helper.setText(R.id.tv_goods_num, String.format("x%d", goods.getStockQuantity()));

        helper.setText(R.id.tv_specs, getGoodsSku(goods));

        FrameLayout goodsCb = helper.getView(R.id.shop_cb_fl);
        goodsCb.setSelected(goods.isSelect());
        goodsCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goods.setSelect(!goodsCb.isSelected());
                notifyItemChanged(helper.getAdapterPosition(), helper.getAdapterPosition());
                if (mListener != null){
                    mListener.onItemClick(goods, helper.getAdapterPosition());
                }
            }
        });

        helper.getView(R.id.goods_item).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GoodsDetailsActivity.Companion.self(helper.itemView.getContext(), goods.getId());
            }
        });
    }

    private String getGoodsSku(GoodsEntity goods) {
        StringBuffer sb = new StringBuffer();
        if (!TextUtils.isEmpty(goods.getColor())){
            sb.append("颜色-").append(goods.getColor());
        }
        if (!TextUtils.isEmpty(goods.getSize())){
            sb.append("；  ").append("尺寸-").append(goods.getSize());
        }
        return sb.toString();
    }

    public interface OnGoodsListAdapterListener{
        void onItemClick(GoodsEntity goodsEntity, int position);
    }
}
